package com.sky.cqculture.Contraller;

import com.sky.cqculture.pojo.Order;
import com.sky.cqculture.pojo.Result;
import com.sky.cqculture.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    /**
     * 创建订单 - 简化版接受JSON对象
     */
    @PostMapping("/create")
    public Result createOrder(@RequestBody OrderRequest orderRequest) {
        try {
            // 从请求对象中获取参数
            Integer userId = orderRequest.getUserId();
            List<Integer> cartItemIds = orderRequest.getCartItemIds();
            if (userId == null || userId <= 0) {
                return Result.error("用户ID不能为空");
            }
            Order order = orderService.createOrder(userId, cartItemIds);
            return Result.success(order);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建订单失败：" + e.getMessage());
        }
    }

    // 定义一个简单的请求对象类
    static class OrderRequest {
        private Integer userId;
        private List<Integer> cartItemIds;

        // getters and setters
        public Integer getUserId() { return userId; }
        public void setUserId(Integer userId) { this.userId = userId; }
        public List<Integer> getCartItemIds() { return cartItemIds; }
        public void setCartItemIds(List<Integer> cartItemIds) { this.cartItemIds = cartItemIds; }
    }
}